package com.cd.university.service.impl;

import com.cd.university.common.RestResult;
import com.cd.university.common.UniversityConstants;
import com.cd.university.common.enums.ResponseEnum;
import com.cd.university.controller.form.ProjectTeamForm;
import com.cd.university.db.mapper.ProjectMapper;
import com.cd.university.db.mapper.ProjectTeamMapper;
import com.cd.university.db.mapper.UserMapper;
import com.cd.university.db.pojo.Message;
import com.cd.university.db.pojo.Project;
import com.cd.university.db.pojo.ProjectTeam;
import com.cd.university.db.pojo.User;
import com.cd.university.service.MessageService;
import com.cd.university.service.ProjectService;
import com.cd.university.service.ProjectTeamService;
import com.cd.university.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 陈栋
 * @create 2021/10/15 20:06
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProjectTeamServiceImpl implements ProjectTeamService {

    @Autowired
    private ProjectTeamMapper projectTeamMapper;

    @Autowired
    private MessageService messageService;

    // TODO: 2021/11/24 违规操作，直接调用ProjectMapper
    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public RestResult insertProjectTeam(List<ProjectTeamForm> projectTeamFormList) {

        int count = 0;

        for (ProjectTeamForm projectTeamForm : projectTeamFormList) {
            ProjectTeam projectTeam = projectTeamFormToProjectTeam(projectTeamForm);
            projectTeam.setStatus(UniversityConstants.PROJECT_TEAM_NOT_ACCEPTED);
            projectTeamMapper.insert(projectTeam);
            count++;
        }

        ProjectTeamForm form = projectTeamFormList.get(0);
        Project project = projectMapper.selectByPrimaryKey(form.getProjectId());
        if (project==null) {
            return RestResult.failure("项目不存在，请重新创建");
        }

        String projectName = project.getProjectName();
        messageService.teamInvitation(new Message(null,form.getUserId(),
                form.getProjectId(),
                projectName,
                null,null,null,null,UniversityConstants.MESSAGE_STATUS_INVITE,null));

        return RestResult.success("插入" + count + "数据成功");
    }

    @Override
    public RestResult searchProjectUsers(Integer projectId) {
        List<User> users = projectTeamMapper.searchUsersByProjectId(projectId);

        if (users==null || users.size()==0) {
            return RestResult.failure(ResponseEnum.PROJECT_NOT_EXISTS.getMsg());
        }

        List<UserVo> userVos = userListToUserVoList(users);
        return RestResult.success("该项目的用户成员如下")
                .put("data",userVos);
    }

    private ProjectTeam projectTeamFormToProjectTeam(ProjectTeamForm form) {
        ProjectTeam projectTeam = new ProjectTeam();

        BeanUtils.copyProperties(form, projectTeam);
        return projectTeam;
    }

    /**
     * 把用户的pojo类转成vo对象
     * @param users 用户的pojo集合
     * @return Vo对象集合
     */
    public List<UserVo> userListToUserVoList(List<User> users) {
        ArrayList<UserVo> userVos = new ArrayList<>();

        for (User user : users) {
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(user,userVo);
            userVos.add(userVo);
        }

        return userVos;
    }
}
